<?php
namespace jy_area_psi;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	public function check_member(){
		$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		if(!$member['id']){
			$this->register();
			$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		}
		$weishop_member=pdo_get('jy_weishop_member',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		$member=array_merge($weishop_member,$member);
		$member['newlevel_id']=pdo_getcolumn('jy_area_psi_level',array('shoplevelid'=>$weishop_member['level'],'uniacid'=>$_SESSION['uniacid']),'id');
		return $member;
	}
	/*
		会员注册
	*/
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('区域代理会员注册失败，缺少会员ID！');
		}
		$ismid=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid']),array('mid'));
		if($ismid){
			return false;
		}
		$weishop_member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'fromuser');
		$agent_id=$weishop_member['fromuser'];
		$agent_id=$agent_id?:$_SESSION['fromuser'];
		if(!$agent_id && $this->setting['agent_id'] && ($_SESSION['container']!='wxapp' || $_SESSION['fromh5wxapp'])){
			$agent_id=$this->setting['agent_id'];
		}
		$member['agent_id']=$agent_id;
		$this->insert('member',$member);
		if($this->plugin_exists('jy_yangjidan')){
			model('jy_yangjidan/model')->upmember($member['mid'],$agent_id);
			return false;
		}
	}
	public function check_area($member=array()){
		if(!$member){
			$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		}
		if((!$member['is_agent'] || $member['stop'] || !$this->setting['level']) && $_RQ['action'] !='apply' && $_RQ['action'] !='yajin_log'){
			exi('','','jy_area_psi/apply/');
		}
		$agent_area=$this->get('agent_area',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		return $agent_area;
	}
	/*
		申请成为区域代理
	*/
	public function apply_agent($mid,$area_code,$post=array()){
		if(!$area_code && !$post['is_duodai']){
			exi('获取区域code失败！','error');
		}
		$check=$this->getcolumn('agent_area',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'area_code'=>$area_code),'id');
		if($check){
			exi('您已经代理了该地区，请勿重复代理！','error');
		}
		$check=$this->getcolumn('agent_area',array('uniacid'=>$_SESSION['uniacid'],'area_code'=>$area_code),'id');
		if($check){
			exi('该区域已存在代理！请更换！','error');
		}
		$agent=$this->getcolumn('agent_area',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid),'id');
		if($agent){
			exi('您已经是代理商了，不能重复申请！','error');
		}
		if($post['price']>0){
			$is_yajinfee=pdo_getcolumn('jy_area_psi_agent_area',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid),'is_yajinfee');
			if(!$is_yajinfee || $is_yajinfee<=0){
				$status=3;
			}
		}
		$weishop_member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$mid),'fromuser');
		$fromuser=$weishop_member['fromuser'];
		$fromuser=$fromuser?:$_SESSION['fromuser'];
		if(!$fromuser && $this->setting['agent_id'] && ($_SESSION['container']!='wxapp' || $_SESSION['fromh5wxapp'])){
			$fromuser=$this->setting['agent_id'];
		}
		$new_apply=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'status'=>$status,
			'area_type'=>pdo_getcolumn('core_area',array('code'=>$area_code),'type'),
			'area_code'=>$area_code,
			'fromuser'=>$fromuser,
			'area'=>ijson_encode($post['area'],true),
			'createtime'=>TIMESTAMP,
		);
		$this->insert('agent_area',$new_apply);
		$id=$this->insertid();
		return $id;
	}
	public function up_agent($mid,$area_code,$post=array()){
		if(!$area_code && !$post['is_duodai']){
			//exi('请选择区域省份！','error');
		}
		$new_apply=array();
		if($area_code){
			$new_apply['area_code']	=$area_code;
			$new_apply['area_type']	=pdo_getcolumn('core_area',array('code'=>$area_code),'type');
		}
		$new_apply['duodai']	=$post['duodai'];
		$new_apply['is_duodai']	=$post['is_duodai'];
		$new_apply['area']=ijson_encode($post['area']);
		$this->update('agent_area',$new_apply,array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['id']));
		return $id;
	}
	/*
		审核区域代理
	*/
	public function check_agent($agent_id,$status,$remark=''){
		if(!$agent_id || !$status){
			return;
		}
		$agent_area=$this->get('agent_area',array('id'=>$agent_id));
		if(!$agent_area){
			return;
		}
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，如果会员还不是代理商，此处修改
			$this->update('member',array('is_agent'=>1),array('mid'=>$agent_area['mid'],'is_agent'=>0));
		}else{
			$update['remark']=$remark;
		}
		$this->update('agent_area',$update,array('id'=>$agent_id));
		if($update['status']==1){
			$this->model('notice')->notice_fenxiao_add($agent_area['mid']);
		}
		return true;
	}
	public function set_parent($mid,$fromuser){
		$this->update('member',array('agent_id'=>$fromuser),array('mid'=>$mid));
		$this->model('notice')->notice_fenxiao_xiajiadd($mid,$fromuser);
		return true;
	}
	/*
		禁用代理商
	*/
	public function stop_agent($agent_id){
		$this->update('member',array('stop'=>1),array('mid'=>$agent_id));
	}
	/*
		恢复代理商
	*/
	public function start_agent($agent_id){
		$this->update('member',array('stop'=>0),array('mid'=>$agent_id));
	}

	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($agent_id=0){
		//
		$agent=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$agent_id,'merchid'=>0));
		if($agent['level']){
			$level=pdo_get('jy_area_psi_level',array('uniacid'=>$_SESSION['uniacid'],'shoplevelid'=>$agent['level']));
		}else{
			$level['level']=0;
		}
		//file_put_contents(ROOT_D.'/1.txt',$level['level'].'~~~'.$agent['level']);
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'price <'=>0.1,'level >'=>$level['level']),
			'order_by'=>'level desc',
		);
		$levels=pdo_getall('jy_area_psi_level',$parameter['params'],'','',$parameter['orderby']);
		if(empty($levels)){
			return;
		}
		$level_up_types=$this->model()->current_level_up_types();//获取当前设置的分销商等级升级条件
		if(!$level_up_types){
			return;
		}
		foreach($levels as $lv){
			$conditions=json_decode($lv['conditions'],true);
			if(!$lv['id']){
				break;
			}
			$up=true;
			foreach($level_up_types as $type){
				if($type=='buy_goods' && count($conditions['goods_ids'])){
					
					$goods_ids=$conditions['goods_ids'];
					foreach($goods_ids as $gv){
						$up=false;
						$isbuynum=pdo_getsum('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$agent['mid'],'goodsid'=>$gv['id']),'total');
						//file_put_contents(ROOT_D.'/1.txt','~isbuynum~~'.$isbuynum."\n",FILE_APPEND);
						if($isbuynum>0){
							$up=true;
							break;
						}
					}
					if($up){
						break;
					}
				}
				if($type=='level_num' && $conditions['level_num']){
					if(strpos($conditions['level_id'], 'a') !== false){
						$member['level_num']=pdo_count('jy_area_psi_agent_area',array('uniacid'=>$_SESSION['uniacid'],'level_id'=>$conditions['level_id'],'fromuser'=>$agent['mid']));
					}else{
						$shoplevelid=pdo_getcolumn('jy_area_psi_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$conditions['level_id']),'shoplevelid');
						$member['level_num']=pdo_count('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'level'=>$shoplevelid?:0,'fromuser'=>$agent['mid']));
					}
					if($member['level_num']<$conditions['level_num']){
						$up=false;
						break;
					}
				}
				if($type=='myorder_price' && $conditions['myorder_price']){
					$member['myorder_price']=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$agent['mid']),'price');
					if($member['myorder_price']<$conditions['myorder_price']){
						$up=false;
						break;
					}
				}
				if($type=='order_num' && $conditions['order_num']){
					$member['order_num']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$agent['mid']));
					if($member['order_num']<$conditions['order_num']){
						$up=false;
						break;
					}
				}
				if($type=='yang_jinum' && $conditions['yang_jinum']){
					$member['yang_jinum']=pdo_count('jy_yangjidan_myji',array('mid'=>$agent['mid'],'uniacid'=>$_SESSION['uniacid']));
					if($member['yang_jinum']<$conditions['yang_jinum']){
						$up=false;
						break;
					}
				}
			}
			if($up){
				//file_put_contents(ROOT_D.'/1.txt','~~~'.$lv['title']."\n",FILE_APPEND);
				$next_level=$lv;
				break;
			}
		}
		if($up){
			$shoplevelid=pdo_getcolumn('jy_area_psi_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$next_level['id']),'shoplevelid');
			$update=array(
				'level'=>$shoplevelid,
			);
			pdo_update('jy_weishop_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$agent['id']));
			if($agent['fromuser']){
				$this->update_agent_level($agent['fromuser']);
			}
		}
		return true;
	}
	public function koufenfee($feeorder){
		if(!$this->setting['open_koufenfee']){
			return;
		}
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ordersn'=>$feeorder['ordersn']),array('id','ordersn','address','addressid'));
		$order['address']=json_decode($order['address'],true);
        if($order['address'] && ($order['address']['name'] || $order['address']['adddress'])){
            $address=$order['address'];
        }else{
            $address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']),'pcd');
        }
		$address=$address['pcd'];
        if(!is_array($address)){
            $address=json_decode($address,true);
        }
		if(!$address){
			return;
		}
		$allagents=array();
		$agents1=$this->getall('agent_area',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'is_duodai'=>1,'duodai like'=>'%'.trim($address['province']).'%'));
		$area_code=pdo_getcolumn('core_area',array('address'=>$address['province']),'code');
		$this->commission_order['area_code']=$area_code;
		$agents2=$this->getall('agent_area',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'area_code'=>$area_code));
		foreach($agents1 as $item){
			$allagents[$item['mid']]=$item['mid'];
		}
		foreach($agents2 as $item){
			$allagents[$item['mid']]=$item['mid'];
		}
		$num=count($allagents);
		if(!$num){
			return;
		}
		$onefee=$feeorder['money'];
		if(!$onefee){
			return;
		}
		foreach($allagents as $mid){
			$more=array('ordersn'=>$feeorder['ordersn']);
			model('core/member')->member_credit($mid,'credit2',$onefee,false,'jy_area_psi','省代订单佣金扣','','',$more);
		}
		return;
	}
}